import React, { useCallback } from 'react'
import $axios from '@/plugins/request'
import { withRouter } from 'react-router';
import ReactDOM from 'react-router-dom'
import PropTypes from "prop-types";
import { useState } from 'react';
import { withUser } from '@/plugins/hoc'
import moment from 'moment';
import {
  Form,
  Input,
  Button,
  Select,
  message,
  DatePicker, Card
} from 'antd';
const { Option } = Select
const { RangePicker } = DatePicker;
function UserForm({ history, data }) {
  const [form] = Form.useForm()
  const isEdit = !!data
  const rules = ({
    branch: [
      {
        required: true,
        message: '请输入所属分支',
        trigger: "onBlur"
      },
      {
        pattern: new RegExp(/^[a-zA-Z0-9\u4e00-\u9fa5]{2,20}$/),
        message: '请输入正确的分支',
        trigger: ["onBlur"]
      }
    ],
  })
  const onFinish = useCallback(async ({ _id, ...values }) => {
    if (isEdit) {
      const { data } = await $axios.put("/api/branch/" + _id, values)
      if (data.code == 1) {
        return message.success("支部信息修改成功")
      }
    } else {
      const { data } = await $axios.post('/api/branch/0', {
        ...values
      })
      if (data.code == 1) {
        message.success("支部信息添加成功")
        history.push('/manage/branch/list')
      } else {
        message.warning(data.msg)
      }
    }
  }, [])
  // const onChange=useCallback(()=>{
  //   let values=form.getFieldsValue()
  //   changeSubmitData(values, 'editData')
  // })
  //编辑
  setTimeout(() => {
    if (isEdit) {
      data.join_time = data?.join_time ? moment(data?.join_time) : null
      data.zz_time = data?.zz_time ? moment(data?.zz_time) : null
      form.setFieldsValue(data)
    }
  })


  return (
    <Card title="支部信息相关操作" style={{ width: 600, marginLeft: 100 }}>
      <Form
        labelCol={{
          span: 8,
        }}
        wrapperCol={{
          span: 14,
        }}
        layout="horizontal"
        form={form}
        onFinish={onFinish}
        style={{ width: 450 }}
      >
        <Form.Item label="_id" name="_id" hidden>
          <Input />
        </Form.Item>

        <Form.Item label="分支名称" name="branch" rules={rules.branch} >
          <Input />
        </Form.Item>
        <Form.Item wrapperCol={{
          offset: 8,
        }}>
          <Button type="primary" htmlType="submit" style={{ marginRight: 15 }} >
            提交
          </Button>
          <Button type="primary" onClick={() => {
            history.goBack();
          }}>
            返回
          </Button>
        </Form.Item>
      </Form>
    </Card>

  )
}

UserForm = withUser(UserForm)
UserForm = withRouter(UserForm)
export default UserForm